package com.xinke.module.system.controller.admin.social;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.xinke.component.common.pojo.PageParam;
import com.xinke.component.common.pojo.PageResult;
import com.xinke.component.common.pojo.Result;
import com.xinke.component.common.util.object.BeanUtils;
import static com.xinke.component.common.pojo.Result.success;

import com.xinke.component.excel.excel.core.util.ExcelUtils;

import com.xinke.component.web.apilog.core.annotation.ApiAccessLog;
import static com.xinke.component.web.apilog.core.enums.OperateTypeEnum.*;

import com.xinke.module.system.model.vo.social.*;
import com.xinke.module.system.model.entity.social.SocialUserBindDO;
import com.xinke.module.system.service.social.SocialUserBindService;

@Tag(name = "管理后台 - 社交绑定")
@RestController
@RequestMapping("/system/social-user-bind")
@Validated
public class SocialUserBindController {

    @Resource
    private SocialUserBindService socialUserBindService;

    @PostMapping("/create")
    @Operation(summary = "创建社交绑定")
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:create')")
    public Result<Long> createSocialUserBind(@Valid @RequestBody SocialUserBindSaveReqVO createReqVO) {
        return success(socialUserBindService.createSocialUserBind(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新社交绑定")
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:update')")
    public Result<Boolean> updateSocialUserBind(@Valid @RequestBody SocialUserBindSaveReqVO updateReqVO) {
        socialUserBindService.updateSocialUserBind(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除社交绑定")
    @Parameter(name = "bindId", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:delete')")
    public Result<Boolean> deleteSocialUserBind(@RequestParam("bindId") Long bindId) {
        socialUserBindService.deleteSocialUserBind(bindId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得社交绑定")
    @Parameter(name = "bindId", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:query')")
    public Result<SocialUserBindRespVO> getSocialUserBind(@RequestParam("bindId") Long bindId) {
        SocialUserBindDO socialUserBind = socialUserBindService.getSocialUserBind(bindId);
        return success(BeanUtils.toBean(socialUserBind, SocialUserBindRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得社交绑定分页")
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:query')")
    public Result<PageResult<SocialUserBindRespVO>> getSocialUserBindPage(@Valid SocialUserBindPageReqVO pageReqVO) {
        PageResult<SocialUserBindDO> pageResult = socialUserBindService.getSocialUserBindPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SocialUserBindRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出社交绑定 Excel")
    @PreAuthorize("@ss.hasPermission('system:social-user-bind:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSocialUserBindExcel(@Valid SocialUserBindPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SocialUserBindDO> list = socialUserBindService.getSocialUserBindPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "社交绑定.xls", "数据", SocialUserBindRespVO.class,
                        BeanUtils.toBean(list, SocialUserBindRespVO.class));
    }

}